﻿Imports System.Data.SqlClient

Public Class frmCatalogoRoles
    Private mstrCondicion As String
    Private mstrValor As String
    Public WriteOnly Property Condicion() As String
        Set(ByVal value As String)
            mstrCondicion = value
        End Set
    End Property
    Public ReadOnly Property Valor() As String
        Get
            Valor = mstrValor
        End Get
    End Property
    Private Sub frmCatalogoRoles_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ToolStrip1.Cursor = Cursors.Hand
        ToolStrip1.RenderMode = System.Windows.Forms.ToolStripRenderMode.System

        CargaRoles()

    End Sub
    Sub CargaRoles()
        Windows.Forms.Cursor.Current = Cursors.WaitCursor
        dgRoles.Rows.Clear()

        Dim strNombre As String
        Dim cnconn As New SqlConnection
        Dim strSql As String = ""
        Dim cmdBuscar As New SqlCommand
        Dim cmdAsignar As New SqlCommand
        Dim rdBuscar As SqlDataReader
        Dim fecha As Date

        Try
            strNombre = UCase(txtBusqueda.Text.Trim)
            strNombre = "%" & strNombre.Replace(" ", "%") & "%"

            cnconn.ConnectionString = CitraConnection
            cnconn.Open()

            cmdBuscar.Connection = cnconn
            cmdAsignar.Connection = cnconn

            strSql = "Select * " & _
                     "From RolesDescuentos " & _
                     "WHERE upper(Nombre) Like '" & strNombre & "' " & mstrCondicion & " OR UPPER(TipoDia) like '" & strNombre & "' " & mstrCondicion & " Order By TipoDia,Nombre"

            cmdBuscar.CommandText = strSql
            rdBuscar = cmdBuscar.ExecuteReader

            Dim TipoDescuento As String


            Do While rdBuscar.Read()
                If rdBuscar("TipoDescuento") = 0 Then
                    TipoDescuento = "Por porcentaje"
                Else
                    TipoDescuento = "Por marcas"
                End If
                dgRoles.Rows.Add(rdBuscar("IdServicio"), rdBuscar("Nombre"), rdBuscar("HoraInicio"), rdBuscar("HoraTermino"), TipoDescuento, rdBuscar("Marcas"), rdBuscar("PorcentajeMarcas"), rdBuscar("PorcentajeDescuento"), rdBuscar("TipoDia"))
            Loop
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Aviso")
        Finally
            If cnconn.State = ConnectionState.Open Then
                cnconn.Close()
            End If
        End Try



    End Sub

    Private Sub txtBusqueda_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtBusqueda.TextChanged
        CargaRoles()
    End Sub

    Private Sub ToolStripButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton2.Click
        frmRolesDescuentos.ShowDialog()
    End Sub

    Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
        Close()

    End Sub

    Private Sub ToolStripButton3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton3.Click
        IdServicio = 0
        frmRegistraRoles.ShowDialog()
    End Sub

    Private Sub dgRoles_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgRoles.CellContentClick
       
    End Sub

    Private Sub dgRoles_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgRoles.CellDoubleClick
        IdServicio = dgRoles.CurrentRow.Cells(0).Value
        frmRegistraRoles.ShowDialog()
    End Sub
End Class